from microbit import *
from asr import *

# 头积木事件
E = {
    # 按键
    'a': [],
    'b': [],
    'a_and_b': [],
    'a_or_b': [],
    # 徽章
    'touched': [],
    'released': [],
    # 无限循环积木
    'forever': [],
    # 语音识别
    'asr': {}
}


def add_event(type, func, id=-1):
    if type == 'asr' and id > 0:
        m = E['asr']
        if id in m:
            m[id].append(func)
        else:
            m[id] = []
            m[id].append(func)
    else:
        E[type].append(func)


def has_button_event():
    return len(E['a_and_b']) > 0 or len(E['a_or_b']) > 0 or len(E['a']) > 0 or len(
        E['b']) > 0


def has_logo_event():
    return len(E['touched']) > 0 or len(E['released']) > 0


def has_forever_event():
    return len(E['forever']) > 0


def has_asr_event():
    return len(E['asr']) > 0


prev_a_prs = False
prev_b_prs = False
def on_button():
    if has_button_event():
        global prev_a_prs, prev_b_prs
        while True:
            a_prs = button_a.is_pressed()
            b_prs = button_b.is_pressed()
            # 按下再松开触发一次事件
            a_clk = prev_a_prs and not a_prs
            b_clk = prev_b_prs and not b_prs
            a_b_clk = a_clk and b_clk
            if not a_b_clk and (a_clk and b_prs or b_clk and a_prs):
                sleep(0.06)
                a_prs = button_a.is_pressed()
                b_prs = button_b.is_pressed()
                a_b_clk = not a_prs and not b_prs
            list1=[]
            list2=[]
            if a_b_clk:
                list1 = E['a_and_b']
            elif a_clk:
                list1 = E['a']
                list2 = E['a_or_b']
            elif b_clk:
                list1 = E['b']
                list2 = E['a_or_b']
            for fun in (list1+list2):
                try:
                    yield fun()
                except:
                    pass
            prev_a_prs = a_prs
            prev_b_prs = b_prs
            yield


prev_touched = False


def on_logo():
    if has_logo_event():
        global prev_touched, prev_time
        while True:
            touched = pin_logo.is_touched()
            # 触摸：当手指触碰到徽章即生效
            if prev_touched == False and touched:
                for fun in E['touched']:
                    yield fun()
            # 松开：当手指松开的一瞬间
            elif prev_touched and touched == False:
                for fun in E['released']:
                    yield fun()
            else:
                yield
            prev_touched = touched


def on_forever():
    if has_forever_event():
        while True:
            for fun in E['forever']:
                try:
                    yield fun()
                except:
                    pass
            yield


def on_asr():
    if has_asr_event():
        while True:
            id = asr.get_ASR()
            if id and id > 0:
                m = E['asr']
                try:
                    for func in m[id]:
                        yield func()
                except:
                    yield
            else:
                 yield

def handle_logo():
    if pin_logo.is_touched():
        for fun in E['touched']:
            yield fun()

button_cor = on_button()
logo_cor = on_logo()
while_for = on_forever()
asr_cor = on_asr()

def coroutine_init():
    while True:
        if has_button_event():
            button_cor.send(None)
        if has_forever_event():
            while_for.send(None)
        if has_asr_event():
            asr_cor.send(None)
        if has_logo_event():
            logo_cor.send(None)
